/**
 * 注册校验
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2014-09-05 11:10:42
 * @update  2014-09-05 11:10:42
 * @for register.jsp
 * @version $Id$
 */
var BSTYUN = BSTYUN || {};
BSTYUN.check = {
    //IE6判定
    isIE6: window.VBArray && !window.XMLHttpRequest,
    //判断是否为中英文，数字，下划线，减号
    isNick: function(str) {
        var nickReg = /^[\u4e00-\u9fa5A-Za-z0-9-_]+$/;
        return nickReg.test(str);
    },
    //判断邮箱
    isEmail: function(str) {
        var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
        return emailReg.test(str);
    },
    //判断手机
    isMobile: function(str) {
        var mobileReg = /^1[345678][0-9]{9}$/;
        return mobileReg.test(str);
    },
    // 判断固话
    isTelephone: function(str) {
        var phoneReg = /^0\d{2,3}-\d{5,9}$/;
        return phoneReg.test(str);
    },
    //判断URL
    isUrl: function(str) {
        var urlReg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$/;
        return urlReg.test(str);
    },
    //判断数字
    isNum: function(str) {
        var numReg = /^[0-9]\d*$/;
        return numReg.test(str);
    }
};
BSTYUN.forgetPassword = {
    $inputs: null,
    flag: {
        email: false,
        imageCode: false
    },
    _timer: null,
    /*全局定时器*/
    init: function() {
        var that = this;
        /*获取input元素*/
        that.$inputs = $('.input_box').find('input');
        /*检测信息*/
        that.checkInfor();
        /*提交确认*/
        that.confirmSubmit();
        /*图片验证码替换*/
        that.changeImgCode();
    },
    checkInfor: function() {
        var that = this;
        /*获取焦点，清空错误信息*/
        that.$inputs.on('focus', function(event) {
            that.clearErrorMsg($(this));
            /*失去焦点，检测信息正确性*/
        }).on('blur', function(event) {
            var $this = $(this),
                typename = $this.attr('name'),
                val = $.trim($this.val());
            /*输入内容为空，则提示不能为空*/
            if (val == '' || val.length == 0) {
                that.addErrorMsg($this, '不能为空');
                return false;
            }
            if (typename == 'email') {
            	var regWay = val.indexOf("@");
            	if(regWay==-1){
            		 if (!BSTYUN.check.isMobile(val)) {
                         that.addErrorMsg($this, '手机号码格式不正确');
                     } else {
                         /*ajax判断手机号码是否存在*/
                         $.ajax({
                             cache: false,
                             type: "POST",
                             url: "/user/bound/selectUserPsdByBoundTel?random=" + Math.random(),
                             data: {
                                 boundTel: val
                             },
                             async: false,
                             //dataType: 'text',
                             success: function(data) {
                                 if (data == true) {
                                     that.addErrorMsg($this, '该手机未绑定');
                                 } else {
                                     //alert('账号存在,提交重置')
                                     that.clearErrorMsg($this);
                                 }
                             },
                             error: function(request) {
                            	 BSTYUN.dialog.ok("请求失败！请重试");
                                 return
                             }
                         });
                     }
            	}else{
            		 if (!BSTYUN.check.isEmail(val)) {
                         that.addErrorMsg($this, '邮箱格式不正确');
                     } else {
                         /*ajax判断用户名是否存在*/
                         $.ajax({
                             cache: false,
                             type: "POST",
                             url: "/user/bound/selectUserPsdByBoundEmail?random=" + Math.random(),
                             data: {
                                 boundEmail: val
                             },
                             async: false,
                             //dataType: 'text',
                             success: function(data) {
                                 if (data == true) {
                                     that.addErrorMsg($this, '该邮箱未绑定');
                                 } else {
                                     //alert('账号存在,提交重置')
                                     that.clearErrorMsg($this);
                                 }
                             },
                             error: function(request) {
                            	 BSTYUN.dialog.ok("请求失败！请重试");
                                 return
                             }
                         });
                     }
            	}
            } else if (typename == 'imageCode') {
                //可判断验证码格式对不对。
            };
        });
        /*当输入框改变的时候就检测*/
        /*      that.$inputs.filter('[name="email"]').on('keyup', function(event) {
            var $this = $(this),
                val = $.trim($this.val());
            if (BSTYUN.check.isEmail(val)) {
                $this.triggerHandler('blur');
            }
        });*/
    },
    confirmSubmit: function() {
        var $submit = $("#go2submit"),
            that = this,
            $email = that.$inputs.eq(0),
            $imgcode = that.$inputs.eq(1);
        $submit.on('click', function(event) {
            $.each(that.$inputs, function(index, val) {
                $(this).trigger('blur');
            });
            if (that.flag.email != false && that.flag.imageCode != false) {
                var imageCode = $.trim($imgcode.val());
                var email = $.trim($email.val());
                /*验证图片验证码*/
                $.ajax({
                    cache: false,
                    type: "POST",
                    url : "/user/findPassword?random="+Math.random(),
                    data: {
                        account: email,
                        imageCode: imageCode
                    },
                    async: false,
                    dataType: 'text',
                    success: function(data) {
                    	// 0:验证码错误 1：密码更新失败 2：邮件方式找回密码成功；3：手机方式找回密码成功
                        if (data == 0) {
                            /*验证码错误*/
                            that.addErrorMsg($imgcode, '验证码错误');
//                            $("#loginBtn").hide();
                        } else if (data == 1) {
                            /*密码重置失败*/
                            BSTYUN.dialog.warn('密码重置失败，请稍后再试！');
//                            $("#loginBtn").hide();
                        } else if (data == 2) {
                            /*密码重置成功*/
                            BSTYUN.dialog.ok('密码找回成功，请注意查收邮件！');
//                            $("#loginBtn").show();
                        }else if(data == 3){
                        	 BSTYUN.dialog.ok('密码找回成功，请注意查收手机！');
                        }
                    },
                    error: function(request) {
                        BSTYUN.dialog.warn('请求失败！请重试');
//                        $("#loginBtn").hide();
                        return false;
                    }
                });
            };
        });
    },
    changeImgCode: function() {
        var $change = $("#changeImgCode"),
            $img = $change.prev();
        $change.on('click', function(event) {
            $img.attr('src', '/image/code?random=' + Math.random());
        });
    },
    /*清空$this的错误提示信息*/
    clearErrorMsg: function($this) {
        var $this = $this,
            that = this,
            typename = $this.attr('name');
        if (typename == 'email') {
            that.flag.email = true;
            $this.parent().next().children('.error_msg').remove();
        } else if (typename == 'imageCode') {
            that.flag.imageCode = true;
            $this.parent().next().html('&nbsp;');
        };
    },
    /*显示错误信息*/
    addErrorMsg: function($this, msg) {
        var $this = $this,
            that = this,
            typename = $this.attr('name');
        if (typename == 'email') {
            that.flag.email = false;
            $this.parent().next().html('<span class="error_msg">' + msg + '</span>');
        } else if (typename == 'imageCode') {
            that.flag.imageCode = false;
            $this.parent().next().html('<span class="error_msg">' + msg + '</span>');
        };
    }
}
$(function() {
    BSTYUN.forgetPassword.init();
});